%%  Euler Method and Mass on a Spring


% Physical properties
m = 1;      %kg
k = 10;    %N/m
y_eq=-1;      %m


% Make time vector
dt=.001; %s
% This is the period
T=2*pi/(sqrt(k/m)) %s
t=(0:dt:50*T);

% Create anonymous function
z_p=@(z_current) [z_current(2); -k/m*(z_current(1)-y_eq) ]

%Intialize empty results vector
z_found=zeros(2,length(t));

%Set first point to our initial value
y0=-2 %m
v0=-1 %m/s
z_found(1,1)=y0;
z_found(2,1)=v0;

%Run Euler's method
for ii=1:length(t)-1
   z_cur=z_found(:,ii);   
   z_found(:,ii+1)= z_cur+dt*z_p(z_cur);
end

%Plot results
figure
plot(t,z_found(1,:))
xlabel('time')
ylabel('x')

%Notice the compounding errors with Euler's method and large 'dt'





